import dtale
import pandas as pd
import sys
import re


def csv_handle(path):
    df = pd.read_csv(path)
    d = dtale.show(df)
    d.open_browser()


def feather_handle(path):
    df = pd.read_feather(path)
    d = dtale.show(df)
    d.open_browser()


def h5_handle(path):
    df = pd.read_hdf(path)
    d = dtale.show(df)
    d.open_browser()


def switch(string, path):
    meth_map = {
        'csv': csv_handle,
        'feather': feather_handle,
        'h5': h5_handle,
    }

    method = meth_map.get(string)
    if method:
        method(path)
    else:
        exit(1)


if __name__ == '__main__':
    file = sys.argv[1]
    if len(sys.argv) != 2 or re.match(r'.+\.(csv|feather|h5)$', file) is None:
        print('Usage: please input a csv|feature|hdf5 file to load')
        exit(1)

    opt = file.split('.')[-1]
    switch(opt, file)
    print('dtale web process already start...')
    while True:
        pass
